package com.imut.controller;

import com.imut.entity.Novel;
import com.imut.entity.User;
import com.imut.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author jiangYaPeng
 * @Date 2025-06-11 15:47
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public ResponseEntity<Boolean> login(@RequestBody User user, HttpSession session) {

        User login = userService.login(user);

        if (!ObjectUtils.isEmpty(login)) {
            // 模拟token
            session.setAttribute("token", "128566");
            return ResponseEntity.ok(true);
        } else {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
                    .body(false);
        }
    }

    @PostMapping("/register")
    public ResponseEntity<Boolean> register(@RequestBody User user) {
        return ResponseEntity.ok(userService.register(user));

    }

    @GetMapping("/{id}")
    public ResponseEntity<User> selectById(@PathVariable Long id) {
        return ResponseEntity.ok(userService.selectById(id));
    }

    @GetMapping
    public ResponseEntity<List<User>> selectAll(User user) {
        return ResponseEntity.ok(userService.selectList(user));
    }

    @PutMapping
    public ResponseEntity<Boolean> update(@RequestBody User user) {
        return ResponseEntity.ok(userService.update(user));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Boolean> delete(@PathVariable Long id) {
        return ResponseEntity.ok(userService.deleteById(id));
    }

}
